package SplineComponent
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	import mvc.AbstractView;
	import mvc.Controller;
	
	import mx.containers.Panel;
	import mx.containers.TitleWindow;
	import mx.effects.Move;
	import mx.managers.CursorManager;
	
	import util.Observable;
	
	
	public class SplineComponentView extends AbstractView
	{
		private var motherPanel:Panel;
		//private var slidablePanel1:TitleWindow;
		//private var slidablePanel2:TitleWindow;
		private var drag:Boolean=false;
		private var finalPositionX:Number;
		private var finalPositionY:Number;
		private var slidablePanel1StartY:Number;
		private var slidablePanel1EndY:Number;
		private var slidablePanel2StartX:Number;
		private var slidablePanel2EndX:Number;
		private var panelMover:Move;
		
		private var mystage:Object=new Object;
		public var initialMouseX:Number=0;
		public var initialMouseY:Number=0;
		private var finalMouseX:Number;
		private var finalMouseY:Number;
		private var initialWidth:Number;
		private var initialHeight:Number;
		private var ratio:Number;
		
		[Bindable]
		[Embed(source="Assets/icons/resizeIcon.gif")]
		private var resizeIcon:Class;
		
		
		public function SplineComponentView(m:Observable, c:Controller)
		{
			super(m, c);
			
		}
		
		public function makePanelDraggable(motherPanel:Panel):void
		{
			this.motherPanel=motherPanel;
			motherPanel.addEventListener(MouseEvent.MOUSE_DOWN,evtListner_startDrag);
			motherPanel.addEventListener(MouseEvent.MOUSE_UP,evtListner_stopDrag);
			
		}
		
		private function evtListner_startDrag(event:MouseEvent):void
		{
			if(this.motherPanel.contentMouseX > -2 &&this.motherPanel.contentMouseX <this.motherPanel.width + 13 && this.motherPanel.contentMouseY>-30&&this.motherPanel.contentMouseY<-3)
			{
				//-------------setting up the visibilty criterian(change by anks 5/6/09)-------//
				var visibiltyInfoObj:Object=new Object;
					visibiltyInfoObj.panelVisibilty=false;//function overrided in lower classes
				setPanelVisibilty(visibiltyInfoObj);
				//----------------------------------------------------------------------------//
				this.motherPanel.startDrag();
				
			}
		}
		
		private function evtListner_stopDrag(event:MouseEvent):void
		{
			if(this.motherPanel.contentMouseX > -2 &&this.motherPanel.contentMouseX <this.motherPanel.width + 13 && this.motherPanel.contentMouseY>-30&&this.motherPanel.contentMouseY<-3)
			{
				
				this.motherPanel.stopDrag();
				
			//------sending info about the changed cordinates to the lower class(change by anks 5/6/09)-----//	
				var infoObj:Object=new Object;
					infoObj.finalPanelHeight=this.motherPanel.height;
					infoObj.finalPanelWidth=this.motherPanel.width;
					infoObj.finalPanelX=this.motherPanel.x;
					infoObj.finalPanelY=this.motherPanel.y;
					
					infoObj.initialWidth=this.initialWidth;
					infoObj.initialHeight=this.initialHeight;
					
					infoObj.initialYOfSlidablePanel1=this.motherPanel.y+this.motherPanel.height-100;
					infoObj.finalYOfSlidablePanel1=this.motherPanel.y+this.motherPanel.height-10;
					infoObj.initialXOfSlidablePanel2=this.motherPanel.x+this.motherPanel.width-200;
					infoObj.finalXOfSlidablePanel2=this.motherPanel.x+this.motherPanel.width-10;
					
					infoObj.slidablePanel1X=this.motherPanel.x;
					infoObj.slidablePanel1Y=this.motherPanel.y+this.motherPanel.height-100;
					infoObj.slidablePanel2X=this.motherPanel.x+this.motherPanel.width-200;
					infoObj.slidablePanel2Y=this.motherPanel.y;
					
					
					setNewCordinates(infoObj);//function overrided in lower classes
			//---------------------------------------------------------------------------------------//
				
			//----------------------setting up	visibiltiy criterian---------------------------------//
				var visibiltyInfoObj:Object=new Object;
					visibiltyInfoObj.panelVisibilty=true;//function is overrided in lower classes
				setPanelVisibilty(visibiltyInfoObj);
			//----------------------------------------------------------------------------------------//
			}
			
		}
		
		public  function makePanelResizable():void
		{
			
			this.motherPanel.addEventListener(MouseEvent.MOUSE_DOWN,evtListner_startResizing);
			
		}
		
		private function evtListner_startResizing(evt:MouseEvent):void
		{
			
			if((this.motherPanel.contentMouseX < this.motherPanel.width-10 && this.motherPanel.contentMouseX >this.motherPanel.width-30) && (this.motherPanel.contentMouseY > this.motherPanel.height-50 && this.motherPanel.contentMouseY < this.motherPanel.height-30))
				{
					CursorManager.setCursor(this.resizeIcon);
					
					initialMouseX =evt.stageX;
					initialMouseY = evt.stageY;
					initialWidth = this.motherPanel.width;
					initialHeight= this.motherPanel.height;
					
				}
		}
		
		private function evtListner_setCordinates(event:MouseEvent):void
		{		
			
			
				CursorManager.removeAllCursors();
				
				if(initialMouseX != 0 && initialMouseY != 0)
				{
					
					finalMouseX =event.stageX;
					finalMouseY =event.stageY;
					
					
				
					this.motherPanel.width = this.motherPanel.width + finalMouseX - initialMouseX;
					this.motherPanel.height =this.motherPanel.height + finalMouseY - initialMouseY;
					
					
					
					var ratioX:Number = (this.motherPanel.width - initialWidth)/initialWidth;
					var ratioY:Number = (this.motherPanel.height - initialHeight)/initialHeight;
					
					resizeChildren(ratioX, ratioY);
					initialMouseX = 0;
					initialMouseY = 0;
					
				//-------------sending info about the changed cordinates to the lower class-------------//		
					var infoObj:Object=new Object;
					
					infoObj.finalPanelHeight=this.motherPanel.height;
					infoObj.finalPanelWidth=this.motherPanel.width;
					infoObj.finalPanelX=this.motherPanel.x;
					infoObj.finalPanelY=this.motherPanel.y;
					infoObj.initialWidth=this.initialWidth;
					infoObj.initialHeight=this.initialHeight;
					infoObj.initialYOfSlidablePanel1=this.motherPanel.y+this.motherPanel.height-100;
					infoObj.finalYOfSlidablePanel1=this.motherPanel.y+this.motherPanel.height-10;
					infoObj.initialXOfSlidablePanel2=this.motherPanel.x+this.motherPanel.width-200;
					infoObj.finalXOfSlidablePanel2=this.motherPanel.x+this.motherPanel.width-10;
					infoObj.slidablePanel1X=this.motherPanel.x;
					infoObj.slidablePanel1Y=this.motherPanel.y+this.motherPanel.height-100;
					infoObj.slidablePanel2X=this.motherPanel.x+this.motherPanel.width-200;
					infoObj.slidablePanel2Y=this.motherPanel.y;
					
					setNewCordinates(infoObj);//function overrided in lower classes 
				//--------------------------------------------------------------------------------------//	
				}
		}
		
		public function recieveMouseUpEvent(event:MouseEvent):void
		{
			//-------------------recieving mouseup event from lesson view-----------------------//
			this.evtListner_setCordinates(event);
			
		}
		
	//-------------------changing size of the direct child of main panel----------------------------------------//	
		private function resizeChildren(ratioX:Number,ratioY:Number):void
		{
				var children: Array = this.motherPanel.getChildren();
				
				for(var i:int = 0; i < children.length; i++)
				{
					var obj:DisplayObject;
					
					obj = DisplayObject(children[i]);
					
					obj.x += obj.x * ratioX;
					obj.width += obj.width * ratioX;
					obj.y += obj.y * ratioY;
					obj.height += obj.height * ratioY;
					
				}
		}
		
		//-------------------------------------------------------------------------------------------------//
		
//---------functions which may or may not be overrided in lower class(change by anks 5/6/09-------------------//
		public function setPanelVisibilty(infoObj:Object):void
		{
			//do nothing
			//work done by overrided functions
		}	
		
		public function setNewCordinates(infoObj:Object):void
		{
			//do nothing
			//work done by overrided functions
		}			
//--------------------------------------------------------------------------------------------------------------//	
	}
}